/**
 * Created by Administrator on 2016/9/27.
 */
angular.module("redBookAPP.controller",[])
    .controller("TabsController",function($scope){
        $scope.TabSelect = function (i) {
            var $tabs = $(".tabs a.daohang");
            $tabs.eq(i-1).addClass("word-color").siblings().removeClass("word-color");    
        }
    	$(".daohang").click(function (){
               $(this).addClass("word-color").siblings().removeClass("word-color");
              });
        
    })
    .controller("HomeController",function ($scope, $ionicPopover, HomeFactory) {
    	$(".button").click(function (){
               $(this).addClass("button-select").siblings().removeClass("button-select");
               });
        $ionicPopover.fromTemplateUrl("pop.html",{})
                    .then(function (popover) {
                        $scope.popover=popover;
                    });
        $scope.showMenu=function (event) {
            $scope.popover.show(event);
        };
        //数据监听
        $scope.$on('updateHome',function () {
            $scope.HomeData = HomeFactory.getHomeData();
        });
        //发起请求
        HomeFactory.getHome(1);

        $scope.selectPage=function (i) {
            var btns = $(".sub_header_list a");
            $.each(btns,function (index,ele) {
                ele.className = 'button button-clear';
            });
            btns[i-1].className = 'button button-clear button-select';
            if(i!=2){
                HomeFactory.getHome(i);
            }
            else {
                alert("你还没有登录！");
            }
        }
    })
    //笔记详情
    .controller("XiangqingController",function ($scope, $stateParams, $rootScope, XiangpingFactory, $ionicSlideBoxDelegate) {
        $rootScope.hideTabs = 'tabs-item-hide';
        $scope.$on('$destroy',function () {
            $rootScope.hideTabs ='';
        });
        
        //数据监听
        $scope.$on('updateHome',function () {
            $scope.XiangqingData = XiangpingFactory.getXiangqingData();
            $scope.count=$scope.XiangqingData.images_list.length;
            $scope.huanye = function (gg) {
                $scope.dangqianye=$ionicSlideBoxDelegate.currentIndex();
                $scope.count=$ionicSlideBoxDelegate.slidesCount();
            };
            $ionicSlideBoxDelegate.update();
        });
        $scope.$on('updateHome_s',function () {
            $scope.XiangqingData_xiao = XiangpingFactory.getXiangqingData_xia();
        });
        //发起请求
        XiangpingFactory.getXiangqing();
        XiangpingFactory.getXiangqing_xia(1);

        $scope.dianji=function (index) {
            XiangpingFactory.getXiangqing_xia(index);
        }
    })
    //评论
    .controller("PinglunController",function ($scope, $rootScope, $stateParams, PinglunFactory) {
        $scope.$on('updatePinglun',function () {
            $scope.PinglunData = PinglunFactory.getPinglunData();
        });
        PinglunFactory.getPinglun();
    })
    //发现部分
    .controller("FindController",function ($scope,$ionicSlideBoxDelegate,FindFactory) {
    	 $scope.onClickPage=function (index) {
            $ionicSlideBoxDelegate.slide(index);
      };
        $scope.$on('updateHots',function () {
            $scope.FindData = FindFactory.getFindData();
          $scope.findZhuanjiData = FindFactory.getFindZhuanjiData();
        });
        FindFactory.getFind();
        FindFactory.getFindZhuanji();
    })
    //购买部分
    .controller("PurchaseController",function ($scope,$state,$rootScope,PurchaseFactory) {
     	//数据监听
        $scope.$on('updatePurchase',function () {
            $scope.PurchaseData = PurchaseFactory.getPurchaseData();
            $scope.purchaseXiaData = PurchaseFactory.getPurchaseXiaData();
        });
        //发起请求
        PurchaseFactory.getPurchase(0);
		$scope.purchasePage=function (i) {
               $(".button").click(function (){
               $(this).addClass("button-select").siblings().removeClass("button-select");
            });
           PurchaseFactory.getPurchase(i);
           PurchaseFactory.getPurchaseXia(i);
   }	
    })
    //购买的子部分
    .controller("QuanbuController",function($scope){

    })
    .controller("HufuController",function($scope){

    })
    .controller("CaizhuangController",function($scope){

    })
    .controller("ShishangController",function($scope){

    })
    .controller("JiajuController",function($scope){

    })
    .controller("MuyingController",function($scope){

    })
    .controller("GerenhuliController",function($scope){

    })
    .controller("JiatingqingjieController",function($scope){

    })
    .controller("MeishiController",function($scope){

    })
    .controller("BaojianpinController",function($scope){

    })
    .controller("ShumajiadianController",function($scope){

    })
    //消息部分
    .controller("NewsController",function ($scope,NewsFactory,$rootScope) {
        $scope.$on('updateNews',function () {
            $rootScope.newsData = NewsFactory.getNewsData();
			for(var j=0;j<$rootScope.newsData.length;j++){
				//转换时间
				var time=$rootScope.newsData[j].time;
				var d = new Date(time*1000);
				$scope.dat=d.toLocaleDateString();
				//转换标题
				var titl=$rootScope.newsData[j].title;
				$scope.hl=titl;
			}
			
        });
        //发起请求
        NewsFactory.getNews(0);
		$scope.newsPage=function (i) {
             var btns = $(".nav-news a");
            $.each(btns,function (index,ele) {
                ele.className = 'tab-item';
            });
            btns[i].className = 'tab-item a-active';
           NewsFactory.getNews(i);
   }
    })
    .controller("DongtaiController",function($scope){
    	
    })
    .controller("TongzhiController",function($scope){
    	
    })
    .controller("GuanyuniController",function($scope){
    	
    })
    //我的部分
    .controller("MineController",function ($scope,$ionicSlideBoxDelegate,$ionicScrollDelegate) {
        var $buttonBar1 = $(".buttonBar1 .button");
        var $buttonBar2 = $(".buttonBar2 .button");
        var left = true;

       $scope.zhuoyouhuadong = function () {
           if(left){
               $buttonBar1.eq(1).addClass("mine-xiahuaxian").siblings().removeClass("mine-xiahuaxian");
               $buttonBar2.eq(1).addClass("mine-xiahuaxian").siblings().removeClass("mine-xiahuaxian");
               left = false;
           }
           else {
               $buttonBar1.eq(0).addClass("mine-xiahuaxian").siblings().removeClass("mine-xiahuaxian");
               $buttonBar2.eq(0).addClass("mine-xiahuaxian").siblings().removeClass("mine-xiahuaxian");
               left = true;
           }
       }

       $scope.huanyeSlide = function (index) {
           if (index==1){
               $buttonBar1.eq(0).addClass("mine-xiahuaxian").siblings().removeClass("mine-xiahuaxian");
               $buttonBar2.eq(0).addClass("mine-xiahuaxian").siblings().removeClass("mine-xiahuaxian");
               $ionicSlideBoxDelegate.previous();
           }
           else{
               $buttonBar1.eq(1).addClass("mine-xiahuaxian").siblings().removeClass("mine-xiahuaxian");
               $buttonBar2.eq(1).addClass("mine-xiahuaxian").siblings().removeClass("mine-xiahuaxian");
               $ionicSlideBoxDelegate.next();
           }
       }

       $scope.yemianhuadong = function () {
           var ss = $(".buttonBar2").offset().top;
           if(ss<=41){
               $(".buttonBar1").css("opacity","1");
               $("h2.yonghu-title").css("opacity","1");
           }
           else if(ss>41){
               $(".buttonBar1").css("opacity","0");
               $("h2.yonghu-title").css("opacity","0");
           }
       }
    });
   